import * as VueRouter from 'vue-router'
import routes from "./routes";
import {getCookies} from "../utils";
import NProgress from "nprogress";

const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes,
});


router.beforeEach((to, from, next) => {
    NProgress.start();
    let toPath = to.path
    let isLogin = getCookies();

    if (!isLogin && toPath !== '/login' && toPath !== '/register') {
        next({
            path: '/login'
        })
    } else if (isLogin && (toPath === '/login' || toPath === 'register')) {
        next({
            path: '/'
        })
    } else {
        next()
    }
})
router.afterEach((to, from) => {
    NProgress.done();
})
export default router
